package com.sslpay.common.domain;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 描述：响应类型
 *
 * @author ssl
 * @create 2020/04/30 16:00
 */
@ApiModel(value = "RestResponse<T>", description = "响应通用参数包装")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class RestResponse<T> {

    @ApiModelProperty("响应错误码，0为正常")
    private int code;

    @ApiModelProperty("响应错误消息")
    private String msg;

    @ApiModelProperty("响应内容")
    private T result;


    private RestResponse() {
        this(0, "", null);
    }

    private RestResponse(int code, String msg, T result) {
        this.code = code;
        this.msg = msg;
        this.result = result;
    }

    /**
     * 处理成功
     *
     * @param <T>
     * @return
     */
    public static <T> RestResponse<T> success() {
        return new RestResponse();
    }

    /**
     * 处理成功
     *
     * @param result
     * @param <T>
     * @return
     */
    public static <T> RestResponse<T> success(T result) {
        RestResponse<T> restResponse = success();
        restResponse.setResult(result);
        return restResponse;
    }

    /**
     * 处理失败
     *
     * @param errorCode
     * @param <T>
     * @return
     */
    public static <T> RestResponse<T> fail(ErrorCode errorCode) {
        RestResponse<T> restResponse = new RestResponse<>();
        restResponse.setCode(errorCode.getCode());
        restResponse.setMsg(errorCode.getMsg());
        return restResponse;
    }

    /**
     * 处理失败，系统未知错误
     *
     * @param <T>
     * @return
     */
    public static <T> RestResponse<T> unknown() {
        RestResponse<T> restResponse = new RestResponse<>();
        restResponse.setCode(CommonErrorCode.UNKNOWN.getCode());
        restResponse.setMsg(CommonErrorCode.UNKNOWN.getMsg());
        return restResponse;
    }

    public int getCode() {
        return code;
    }

    public RestResponse setCode(int code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public RestResponse setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getResult() {
        return result;
    }

    public RestResponse setResult(T result) {
        this.result = result;
        return this;
    }
}
